package com.example.course.controller;

import com.example.course.bean.Course;
import com.example.course.bean.Student;
import com.example.course.bean.Teacher;
import com.example.course.bean.User;
import com.example.course.service.StudentService;
import com.example.course.service.TeacherService;
import com.example.course.vo.Result;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class StudentController {
    @Resource
    private StudentService studentService;
    @Resource
    private TeacherService teacherService;
    @ResponseBody
    @RequestMapping("/course/showStudents")
    public Result showStudents() {
        return new Result(studentService.getStudents());
    }

    @ResponseBody
    @RequestMapping("/course/updateStudent")
    public Result updateStudent(Student student) {
        int i = studentService.updateStudent(student);
        if (i == 0) {
            return Result.fail();
        }else {
            return Result.success();
        }

    }

    @ResponseBody
    @RequestMapping("/course/deleteStudent/{id}")
    public Result deleteStudent(@PathVariable int id) {
        int i = studentService.deleteStudent(id);
        if (i == 0) {
            return Result.fail();
        }else {
            return Result.success();
        }

    }

    @ResponseBody
    @RequestMapping("/course/insertStudent")
    public Result insertStudent(Student student) {
        int i = studentService.insertStudent(student);
        if (i == 0) {
            return Result.fail();
        }else {
            return Result.success();
        }

    }
    @ResponseBody
    @RequestMapping("/course/chooseMyCourse/{Id}")
    public Result chooseMyCourse(@PathVariable int id, HttpSession session) {
        User user = (User)session.getAttribute("user");
        Teacher teacher = (Teacher) user.getData();
        List<Student> studentList = studentService.getStudentById(teacher.getId(), id);
        return  new Result(studentList);

    }
    @ResponseBody
    @RequestMapping("/course/application/{courseId}")
    public Result applicationCourse(@PathVariable int courseId, HttpSession session) {
        User user = (User)session.getAttribute("user");
        Student student = (Student) user.getData();
        Teacher teacher = teacherService.getTeacherByCourseId(courseId);

        int i = studentService.applicationCourse(courseId, student.getId(),teacher.getId());
        if (i == 0) {
            return Result.fail();
        }else if (i == -1) {
            return new Result("课程剩余位置不足");
        }else {
            return Result.success();
        }
    }
    @ResponseBody
    @RequestMapping("/course/showMyApplication/{stuId}")
    public Result showMyApplication(@PathVariable int stuId, HttpSession session) {
        User user = (User)session.getAttribute("user");
        Student student = (Student) user.getData();
        List<Course> courseList = studentService.getCourseByStudent(student.getId());
        return  new Result(courseList);

    }
    @ResponseBody
    @RequestMapping("/course/removeApplication/{stuId}/{courseId}")
    public Result showMyApplication(@PathVariable int stuId, @PathVariable int courseId,HttpSession session) {
        User user = (User)session.getAttribute("user");
        Student student = (Student) user.getData();
        int i = studentService.deleteCourseByStudent(student.getId(), courseId);
        if (i == 0) {
            return Result.fail();
        }else {
            return Result.success();
        }
    }
    @ResponseBody
    @RequestMapping("/course/selectStudentById")
    public Result showMyApplication(HttpSession session) {
        User user = (User)session.getAttribute("user");
        Teacher teacher = (Teacher) user.getData();
        List<Student> studentList = studentService.getStudentByTeacherId(teacher.getId());
        return  new Result(studentList);

    }
}
